package com.zsc.yygh.order.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.yygh.common.result.Result;
import com.zsc.yygh.model.order.PaymentInfo;
import com.zsc.yygh.order.service.PaymentService;
import com.zsc.yygh.order.service.WeixinService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/order/weixin")
@Api(tags = "微信支付接口")
public class WeixinController {

    @Autowired
    private WeixinService weixinService;

    @Autowired
    private PaymentService paymentService;

    //生成微信支付二维码
    @GetMapping("createNative/{orderId}")
    public Result createNative(@PathVariable Long orderId) {
        Map map = weixinService.createNative(orderId);
        return Result.ok(map);
    }

    //查询支付状态
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId) {
        //调用微信接口实现支付状态查询
        Map<String,String> resultMap = weixinService.queryPayStatus(orderId);
        //判断
        if(resultMap == null) {
            return Result.fail().message("支付出错");
        }
        if("SUCCESS".equals(resultMap.get("trade_state"))) { //支付成功
            //更新订单状态
            String out_trade_no = resultMap.get("out_trade_no");//订单编码
            paymentService.paySuccess(out_trade_no,resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }

    /**
     * 获取总收益
     * @return
     */
    @GetMapping("/getTotalRevenue")
    public Result getTotalRevenue(){
        BigDecimal total = new BigDecimal("0");
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.select("total_amount");
        List<PaymentInfo> list = paymentService.list(wrapper);
        for (PaymentInfo paymentInfo : list) {
            total = total.add(paymentInfo.getTotalAmount());
        }
        return Result.ok(total);
    }

}
